﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using SDT.Lib.Paging;
using System.Linq.Dynamic;
namespace SDT.Model
{
    public class CustomerManager
    {
        public static bool CheckByEmail(string email)
        {
            try
            {
                SDTDataContext db = new SDTDataContext();
                
                var l = db.Customers.Where(e=>e.LoweredEmail==email.ToLower()).ToList();
                return l.Count > 0;
            }
            catch (Exception exp)
            {
                throw exp;
            }
        }
        public static bool CheckUserName(string username)
        {
            try
            {
                SDTDataContext db = new SDTDataContext();
                var l = db.Customers.Where(e => e.UserName.ToLower() == username.ToLower()).ToList();
                return l.Count > 0;
            }
            catch (Exception exp)
            {
                throw exp;
            }
        }
        public static IPagedList<Customer> GetList(string keyword, string order, string orderDir, int page, int pageSize)
        {
            var dataContext = new SDTDataContext();
            var l = dataContext.Customers
                .Where(a => keyword == string.Empty ? true : (a.FullName.Contains(keyword.ToLower())) || (a.Email.Contains(keyword.ToLower())) || 
                    (a.UserName.Contains(keyword.ToLower())) || (a.Address.Contains(keyword.ToLower())))
                .OrderBy(string.Format("{0} {1}", order, orderDir)).AsQueryable();
            var returnVal = l.ToPagedList(page, pageSize);
            return returnVal;
        }
        public static Customer GetUser(string username)
        {
            try
            {
                SDTDataContext db = new SDTDataContext();
                var obj = db.Customers.Where(e => e.UserName.ToLower() == username.ToLower() || e.Email.ToLower() == username.ToLower()).FirstOrDefault();
                return obj;
            }
            catch (Exception exp)
            {
                throw exp;
            }
        }
    }
}
